package org.upfrost.util;

import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class StringUtils 
{	
	static private Log log = LogFactory.getLog(StringUtils.class);
	
	static public boolean equals(String s1, String s2)
	{
		if (s1 == null || s2 == null)
			return false;
		
		if (s1.equals(s2))
			return true;
		
		return false;
	}
	

	static public String join(Object[] els, String separator)
	{
		if (els == null)
			return "";
		
		StringBuffer res = new StringBuffer();
		boolean first = true;
		for (Object o: els)
		{
			if (first == false)
				res.append(separator);
			else
				first = false;
			
			if (o != null)
				res.append(o.toString());
		}
		return res.toString();		
	}

	
	static public String join(List<? extends Object> list)
	{
		return join(list, ", ");
	}
	
	static public String join(List<? extends Object> list, String separator)
	{
		StringBuffer res = new StringBuffer();
		boolean first = true;
		for (Object o: list)
		{
			if (first == false)
				res.append(separator);
			else
				first = false;
			res.append(o.toString());
		}
		return res.toString();
	}
	
	static public boolean isEmpty(String txt)
	{
		if (txt == null)
			return true;
		
		if (txt.trim().length()==0)
			return true;
		return false;
	}

	static public String ifEmpty(String txt, String textIfEmpty)
	{
		if (txt == null)
			return textIfEmpty;
		
		if (txt.trim().length()==0)
			return textIfEmpty;

		return txt;
	}

	
	static public String capitalize(String str)
	{
		if (str.length()==0)
			return "";
		
		StringBuffer res = new StringBuffer(str.length());
		res.append(Character.toTitleCase(str.charAt(0)));
		if (str.length()>1)
			res.append(str.substring(1));
		return res.toString();
	}
	
	static public String unNullify(String s)
	{
		if (s == null)
			return "";
		return s;
	}

	static public String toString(Exception exc)
	{
		if (exc == null)
			return "";
		
		ByteArrayOutputStream error = new ByteArrayOutputStream();
		exc.printStackTrace(new PrintStream(error));
		return error.toString();
	}
	
}
